// +----------------------------------------------------------------------------
// | 闪速码java短信sdk
// +----------------------------------------------------------------------------
// | Copyright (c) 2014-2021 https://www.shansuma.com
// +----------------------------------------------------------------------------
// | 闪速码短信平台支持网页在线发送、API集成发送、私有化部署三种形式，可免费试用200条
// +----------------------------------------------------------------------------
// | Author: 橘子俊，开发文档请访问 http://sms.shansuma.com/docs
// +----------------------------------------------------------------------------

package com.zlt.util.CodeUtil;

import com.zlt.util.StringUtil;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;


public class CodeUtil {

    //注册 给验证码
    public static String regPhoneCode(String phone) {
        Client client = new Client();
        client.setAppId("hw_11313");     //开发者ID，在【设置】-【开发设置】中获取
        client.setSecretKey("72eefefaccfc5add9090743b909082d4");    //开发者密钥，在【设置】-【开发设置】中获取
        client.setVersion("1.0");

        /**
         *   json格式可在 bejson.com 进行校验
         */
        String singnstr = "闪速码";
        Client.Request request = new Client.Request();
        request.setMethod("sms.message.send");
        request.setBizContent("" +
                "{\"mobile\":[" + phone + "]," +
                "\"type\":1," +
                "\"template_id\":\"ST_2020101100000005\"," +
                "\"sign\":\"" + singnstr + "\"," +
                "\"send_time\":\"" + StringUtil.getCurrentTime() + "\"," +
                "\"params\":" +
                "{\"code\":\"" + StringUtil.randomStr(6) + "\"}" +
                "}");  // 这里是json字符串，send_time 为空时可以为null, params 为空时可以为null,短信签名填写审核后的签名本身，不需要填写签名id
        System.out.println(client.execute(request));
        System.out.println(request.getBizContent());
        return request.getBizContent();
    }

    //登录  验证码
    public static String logPhoneCode(String phone) {
        Client client = new Client();
        client.setAppId("hw_11313");     //开发者ID，在【设置】-【开发设置】中获取
        client.setSecretKey("72eefefaccfc5add9090743b909082d4");    //开发者密钥，在【设置】-【开发设置】中获取
        client.setVersion("1.0");

        /**
         *   json格式可在 bejson.com 进行校验
         */
        String singnstr = "闪速码";
        Client.Request request = new Client.Request();
        request.setMethod("sms.message.send");
        request.setBizContent("" +
                "{\"mobile\":[\"" + phone + "\"]," +
                "\"type\":1," +
                "\"template_id\":\"ST_2020101100000007\"," +
                "\"sign\":\"" + singnstr + "\"," +
                "\"send_time\":\"" + StringUtil.getCurrentTime() + "\"," +
                "\"params\":" +
                "{\"code\":\"" + StringUtil.randomStr(6) + "\"}" +
                "}");  // 这里是json字符串，send_time 为空时可以为null, params 为空时可以为null,短信签名填写审核后的签名本身，不需要填写签名id
        System.out.println(client.execute(request));
        System.out.println(request.getBizContent());
        return request.getBizContent();
    }

    //修改密码  验证码
    public static String modifyPhoneCode(String phone) {
        Client client = new Client();
        client.setAppId("hw_11313");     //开发者ID，在【设置】-【开发设置】中获取
        client.setSecretKey("72eefefaccfc5add9090743b909082d4");    //开发者密钥，在【设置】-【开发设置】中获取
        client.setVersion("1.0");

        /**
         *   json格式可在 bejson.com 进行校验
         */
        String singnstr = "闪速码";
        Client.Request request = new Client.Request();
        request.setMethod("sms.message.send");
        request.setBizContent("" +
                "{\"mobile\":[" + phone + "]," +
                "\"type\":1," +
                "\"template_id\":\"ST_2020101100000004\"," +
                "\"sign\":\"" + singnstr + "\"," +
                "\"send_time\":\"" + StringUtil.getCurrentTime() + "\"," +
                "\"params\":" +
                "{\"code\":\"" + StringUtil.randomStr(6) + "\"}" +
                "}");  // 这里是json字符串，send_time 为空时可以为null, params 为空时可以为null,短信签名填写审核后的签名本身，不需要填写签名id
        System.out.println(client.execute(request));
        System.out.println(request.getBizContent());
        return request.getBizContent();
    }


    //邮件验证码
    public static String sendEmail(String to) {
        SimpleEmail email = new SimpleEmail();
        email.setHostName("smtp.qq.com");//设置发送邮件的服务器
        //ANAUVVXGRPTIYPJK
        email.setAuthentication("1651001995@qq.com", "upwepixfrdehbfbb");//设置账号和密码
        try {
            String content = StringUtil.randomStr(6);
            String subject = "邮箱验证码";
            email.setFrom("1651001995@qq.com", "61招聘");
//			email.setSSLOnConnect(true);//使用安全连接
            email.addTo(to);//设置接收者
            email.addCc("1651001995@qq.com");//设置抄送 抄送给自己就不会提示554 内容不被允许
            email.setSubject(subject); //设置主题
            email.setMsg(content);//设置消息
            email.send();//发送
            return content;
        } catch (EmailException e) {
            e.printStackTrace();
            return null;
        }
    }


}